/*
 *   This File is Part of CuteOgre Game Engine
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	Copyright 2010 Alexandre Pires ( c.alexandre.pires@gmail.com )
 *  Created on: 2010/09/05
 */

#include "Gui/StyleAttribute.h"
#include <QDebug>

QStringList StyleAttribute::mUnitsStr;

StyleAttribute StyleAttribute::null ( QString::null );

void StyleAttribute::setValue ( const QString& rValue ) {

	QString value = rValue;

	while ( value.startsWith ( ' ' ) ) {

		value = value.remove ( 0, 1 );
	}

	mValue = value;
}

bool StyleAttribute::isResource () {
	return ( mValue.toLower ().left ( 9 ).compare ( "resource(" ) == 0 );
}

bool StyleAttribute::isColor () {
	return ( mValue.left ( 1 ).compare ( "#" ) == 0 || mValue.toLower ().left (
			5 ).compare ( "rgba(" ) == 0
			|| mValue.toLower ().left ( 4 ).compare ( "rgb(" ) == 0 );
}

bool StyleAttribute::isNumber () {

	if ( getUnit () == UNIT_UNKNOWN )
		return false;

	return true;
}

bool StyleAttribute::isText () {

	return ! ( isResource () || isNumber () || isColor () );
}

StyleAttribute::AttributeUnits StyleAttribute::getUnit () {

	if ( mUnitsStr.size () == 0 ) {

		mUnitsStr << "in" << "cm" << "mm" << "pt" << "pc" << "px" << "rem"
				<< "%" << "ex" << "gd" << "em" << "vw" << "vh" << "vm" << "ch"
				<< "deg" << "grad" << "rad" << "turn" << "ms" << "s" << "khz"
				<< "hz";

	}

	QString tmp = mValue;

	while ( !tmp.isEmpty () ) {

		QChar ch = tmp[0];

		if ( ch.isLetter () || ch == '%' )
			break;

		tmp = tmp.right ( tmp.length () - 1 );
	}

	if ( tmp.isEmpty () )
		return UNIT_UNKNOWN;

	for ( int i = 0; i < mUnitsStr.size (); i++ ) {

		if ( tmp.compare ( mUnitsStr[i] ) == 0 )
			return AttributeUnits ( i );
	}

	return UNIT_UNKNOWN;
}

float StyleAttribute::getFloat ( StyleAttribute& rOther ) {

	if ( !rOther.isNumber () )
		return 0;

	QString number;
	int offset = 0;

	while ( offset < rOther.mValue.length () ) {

		QChar ch = rOther.mValue[offset];

		if ( ch.isNumber () ) {
			number.append ( ch );
		}

		offset++;
	}

	return number.toFloat ();
}

int StyleAttribute::getInt ( StyleAttribute& rOther ) {

	if ( !rOther.isNumber () )
		return 0;

	QString number;
	int offset = 0;

	while ( offset < rOther.mValue.length () ) {

		QChar ch = rOther.mValue[offset];

		if ( ch.isNumber () ) {
			number.append ( ch );
		}

		offset++;
	}

	return number.toInt ();
}

QColor StyleAttribute::getColor ( StyleAttribute& rOther ) {

	if ( !rOther.isColor () )
		return QColor ();

	if ( rOther.mValue.left ( 1 ).compare ( "#" ) == 0 ) {

		return QColor ( rOther.mValue );

	} else if ( rOther.mValue.toLower ().left ( 5 ).compare ( "rgba(" ) == 0 ) {

		QString colorValues =
				rOther.mValue.remove ( 0, 5 ).remove ( ')' ).remove ( ' ' );

		QStringList values = colorValues.split ( ',' );

		if ( values.size () != 4 )
			return QColor ();

		int r = values[0].toInt ();
		int g = values[1].toInt ();
		int b = values[2].toInt ();
		int a = values[3].toInt ();

		return QColor ( r, g, b, a );

	} else if ( rOther.mValue.toLower ().left ( 4 ).compare ( "rgb(" ) == 0 ) {

		QString colorValues =
				rOther.mValue.remove ( 0, 4 ).remove ( ')' ).remove ( ' ' );

		QStringList values = colorValues.split ( ',' );

		if ( values.size () != 3 )
			return QColor ();

		int r = values[0].toInt ();
		int g = values[1].toInt ();
		int b = values[2].toInt ();

		return QColor ( r, g, b, 255 );
	}

	return QColor ();
}

QStringList StyleAttribute::getResource ( StyleAttribute& rOther ) {

	if ( !rOther.isResource () )
		return QStringList();

	QString resource = rOther.mValue.remove ( 0, 9 ).remove ( ')' ).remove (
			'\'' );

	QStringList tmp = resource.split ( ',' );
	QStringList resources;

	for ( int i = 0; i < tmp.size (); i++ ) {

		QString value = tmp[i].remove ( '\'' );

		while ( value.startsWith ( ' ' ) ) {
			value = value.remove ( 0, 1 );
		}

		while ( value.endsWith ( ' ' ) ) {
			value = value.remove ( value.length () - 1, 1 );
		}

		resources.append ( value );
	}

	return resources;
}
